Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rust: generate test code from schema docstrings #17396

Merged
merged 7 commits into from
Sep 9, 2024

Conversation

redsun82
Copy link
Contributor

@redsun82 redsun82 commented Sep 6, 2024

This generates test source files from code blocks in class docstrings.

By default the test code is generated as is, but it can optionally:

  • be wrapped in a function providing an adequate context using @rust.doc_test_signaure(sig), with sig containing everything following the function name
  • be skipped altogether using @rust.skip_doc_test

So for example an annotation like

@rust.doc_test_signature("<'a, T: Eq>(x: &'a T, y: &'a T) -> &'a T")
class Foo:
    """
    Bla bla, for example:
    ```
    if x == y { x } else { y }
    ```
    """
    ...

will result in the following wrapper:

fn test_foo<'a, T: Eq>(x: &'a T, y: &'a T) -> &'a T {
    // Bla bla, for example:
    if x == y { x } else { y }
}

This generates test source files from code blocks in class docstrings.

By default the test code is generated as is, but it can optionally:
* be wrapped in a function providing an adequate context using
  `@rust.doc_test_function(name, *, lifetimes=(), return_type="()", **kwargs)`,
  with `kwargs` providing both generic and normal params depending on
  capitalization
* be skipped altogether using `@rust.skip_doc_test`

So for example an annotation like
```python
@rust.doc_test_function("foo",
                        lifetimes=("a",),
                        T="Eq",
                        x="&'a T",
                        y="&'a T",
                        return_type="&'a T")
```
will result in the following wrapper:
```rust
fn foo<'a, T: Eq>(x: &'a T, y: &'a T) -> &'a T {
    // example code here
}
```
@redsun82 redsun82 requested a review from a team as a code owner September 6, 2024 12:25
@redsun82 redsun82 added the no-change-note-required This PR does not need a change note label Sep 6, 2024
Copy link
Contributor

@aibaars aibaars left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, thanks!

@redsun82 redsun82 merged commit 9d08d24 into rust-experiment Sep 9, 2024
3 checks passed
@redsun82 redsun82 deleted the redsun82/rust-doctest-gen branch September 9, 2024 10:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
no-change-note-required This PR does not need a change note
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants